function HomeAssistant(argFromPusher) {
	if ( argFromPusher.parent === undefined ) 
		this.parent = 0;
	else 
		this.parent = argFromPusher.parent;
	if ( argFromPusher.title ) 
		this.title = argFromPusher.title;
}

HomeAssistant.prototype = {
	parent: 0,
	title: "Home",
	key: null,
	setup: function() {
		Ares.setupSceneAssistant(this);
		if(this.parent == 0) homeController = this.controller;
		this.makeHeader();
		this.$.collapsible1.setOpen(true);
		this.$.collapsible2.setOpen(false);
		this.$.collapsible3.setOpen(false);
		this.$.collapsible4.setOpen(false);
	},
	activate: function() {
		this.makeMenu();
	},
	cleanup: function() {
		Ares.cleanupSceneAssistant(this);
	},
	makeHeader: function() {
		this.$.header.setTitle(this.title);
		if(this.title.length <= 30)
			this.controller.get("header").style["font-size"] = "16px";
		else if(this.title.length > 30 && this.title.length <= 50)
			this.controller.get("header").style["font-size"] = "12px";
		else
			this.controller.get("header").style["font-size"] = "10px";
	},
	makeMenu: function() {
		var items=[];
		var this1 = this;
		//menu list
		var menu = getStorageObject("menu");
		if(!menu) return;
		var nonEmptyMenu = getStorageObject("nonEmptyMenu");
		if(!nonEmptyMenu) nonEmptyMenu = [];
		items.push({label: "All"});
		menu.each(function(m){
			m.parents.each(function(p) {
				if(p==this1.parent && nonEmptyMenu.indexOf(m.tid) >= 0){
					m.label=m.name;
					m.clazz = "menu"
					items.push(m);
				}
			});
		});
		this.$.menu1.setItems(items);
		// vendor list
		var parentMenu = getStorageObject("parentMenu");
		if(!parentMenu) parentMenu = [];
		if(parentMenu.indexOf(this.parent) == -1){
			db.transaction( function(transaction) {
				transaction.executeSql( 'SELECT distinct vendor FROM products where caty = ?;', [this1.parent],
					function (transaction, result) {
						for (var i=0; i < result.rows.length; i++) {
							var row = result.rows.item(i);
							row.label = row.vendor ? row.vendor : "N/A";
							row.clazz = "vendor";
							items.push(row);
						}
						this1.$.menu1.setItems(items);
					},
					errorHandler.bind(this1));
			});
		}
		this.$.menu2.setItems(this.$.menu2.getItems());
		this.$.menu3.setItems(this.$.menu3.getItems());
		this.$.menu4.setItems(this.$.menu4.getItems());
	},
	menu1Listtap: function(inSender, event) {
		var nextTitle;
		if(this.title == "Home")
				nextTitle = event.item.label;
		else
			nextTitle = this.title + " > " + event.item.label;
		
		if(event.item.label == "All"){
			Mojo.Controller.stageController.pushScene("productList", {ancestor: this.parent, recursive: true, title: nextTitle});
		}else if(event.item.clazz == "menu"){
			Mojo.Controller.stageController.pushScene("home", {parent: event.item.tid, title: nextTitle});
		}else if(event.item.clazz == "vendor"){
			Mojo.Controller.stageController.pushScene("productList", {ancestor: this.parent, vendor: event.item.vendor, recursive: false, title: nextTitle});
		}
	},
	searchBtnTap: function(inSender, event) {
		var key = this.m_key.value;
		var menu = getStorageObject("menu");
		var this1 = this;
		var searchCaty = null;
		menu.each(function(m){
			if(m.name.toUpperCase()==key.toUpperCase()){
				searchCaty = m;
				throw $break;
			}
		});
		if(!searchCaty){
			Mojo.Controller.stageController.pushScene("productList", {ancestor: this.parent, key: key, title: "Search: '" + key + "'"});
			return;
		}
		myAlert(this1, "Do you mean category '" + searchCaty.name + "'?", 
			function(){
				Mojo.Controller.stageController.pushScene("home", {transition:'Mojo.Transition.none',parent: searchCaty.tid, title: "Category: '"+searchCaty.name+"'"});
			},
			function(){
				Mojo.Controller.stageController.pushScene("productList", {transition:'Mojo.Transition.none', ancestor: this1.parent, key: key, title: "Search: '"+key+"'"});
			},
			"Yes", "No"
		);
	},
	menu2Listtap: function(inSender, event) {
		if(event.item.value == "logout"){
			userLogout();
		}else{
			Mojo.Controller.stageController.pushScene(event.item.value, {transition:'Mojo.Transition.none'});
		}
	},
	backBtTap: function(inSender, event) {
		Mojo.Controller.stageController.popScene();
	}
};
